import { postMethod } from './http';
import axios from 'axios';
export function getRandomHex() {
  let result = '';
  for (let i = 0; i < 16; i++) {
    result += Math.floor(Math.random() * 16).toString(16);
  }
  return result;
}
export function getSession(key) {
  return window.sessionStorage.getItem(key);
}
export function setSession(key, value) {
  return window.sessionStorage.setItem(key, value);
}
export function remSession(keys = []) {
  keys.forEach(i => window.sessionStorage.removeItem(i));

}
export function filterSchool(schoolList) {
  const school = getSession('user-info') && getSession('user-info') == 0 ? getSession('user-info') : JSON.parse(getSession('user-info'));
  if (school == 0) return schoolList;
  else return schoolList.filter(i => school.id == i.id);

}

// 对象序列化，undefined和函数丢失问题
export const JSONStringify = (option) => {
  return JSON.stringify(option,
    (key, val) => {
      // 处理函数丢失问题
      if (typeof val === 'function') {
        return `${val}`;
      }
      // 处理undefined丢失问题
      if (typeof val === 'undefined') {
        return 'undefined';
      }
      return val;
    },
    2
  )
}
// 对象序列化解析
export const JSONParse = (objStr) => {
  return JSON.parse(objStr, (k, v) => {
    if (typeof v === 'string' && v.indexOf && v.indexOf('function') > -1) {
      // eval 可能在eslint中报错，需要加入下行注释
      // eslint-disable-next-line
      return eval(`(function(){return ${v}})()`);
    }
    return v;
  });
}




// const instance=axios.create({
//   withCredentials: true
// })
// instance.post('ecp/peiapi/uploadDeliveryImg.json', formData)
// .then(response => {
//  console.log(response.data)
// })
const instance = axios.create({
  withCredentials: true
})
export async function addImage(file) {
  // Assign the url of your endpoint
  const url = 'https://wechat.zhengjkj.com/xyxy/wxapp/public/upload_asset'

  // Construct a FormData object
  const formData = new FormData()

  // Assign the file to formdata
  formData.append('file', file)

  const res = await instance.post(url, formData);
  console.log(res, '0000');
  if (res.data.code === 1) return res.data.data;
  else return null;
}
export async function importData(file, carrierId) {
  // Assign the url of your endpoint
  const url = 'https://wechat.zhengjkj.com/xyxy/wxapp/web/schoolCard/uploadMobile'

  // Construct a FormData object
  const formData = new FormData()

  // Assign the file to formdata
  formData.append('file', file);
  formData.append('carrierId', carrierId);

  const res = await instance.post(url, formData);
  console.log(res, '0000');
  if (res.data.code === 1) return res.data.data;
  else return null;
}


